#include "pch.h"
#include "include/LogInit.h"
#ifdef WIN32
#include <io.h>
#include <direct.h>
#else
#include<unistd.h>
#include <sys/stat.h>
#endif
#include <iostream>
#include <string>

FILE* logfile = NULL;
//-------------------------------------------------------------------------------------
bool Log_Init(const char* appName, const char* cfgFile)
{
    std::string folderpath ="log";
#ifdef WIN32
    if (0 != _access(folderpath.c_str(), 0))
	{
		int ret = _mkdir(folderpath.c_str());
		if (ret < 0)
		{
			return false;
		}
	}
#else
	if (0 != access(folderpath.c_str(), 0))
    {
        int ret = mkdir(folderpath.c_str(), S_IRWXU);
        if (ret < 0)
        {
            return false;
        }
    }
#endif

	time_t tNow = time(NULL);
	tm* pt = localtime(&tNow);
	char buffer1[128] = { 0 };
	char buffer2[128] = { 0 };
	strftime(buffer1, 64, "%Y-%m-%d", pt);
	strftime(buffer2, 64, "%Y-%m-%d %H:%M", pt);

	std::stringstream ss;
	ss << "log/" << appName << "_" << buffer1 << ".log";
	FILE* fp = fopen(ss.str().c_str(), "a");
	if (fp == NULL)
	{
		LogError("Create file %s failed", ss.str().c_str());
		return false;
	}
 
 	fprintf(fp, "\n\n%s\n", buffer2);
	logfile = fp;
return true;
}
void Log_Flush()
{
	if (logfile)
	{
		fflush(logfile);
	}
}
void Log_Uninit()
{
	if (logfile)
	{
		fclose(logfile);
		logfile = NULL;
	}

}

